package com.tuling.controller;

import com.tuling.domain.Fruit;
import com.tuling.domain.User;
import com.tuling.service.FruitService;
import com.tuling.service.FruitServiceImpl;
import com.tuling.service.UserService;
import com.tuling.service.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

@WebServlet(name = "BSServlet", value = "/BSServlet")
public class BsServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    private FruitService fruitService = new FruitServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            request.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setContentType("text/html;charset=utf-8");
        // 获取参数
        String method = request.getParameter("method");
        // 根据method做不同处理
        if (method.equals("toAdmin")) {
            // 添加购物车
            toAdmin(request, response);
        } else if (method.equals("alluser")) {
            allUser(request, response);
        } else if (method.equals("adduser")) {
            addUser(request,response);
        } else if (method.equals("finduser")) {
            findUser(request,response);
        } else if (method.equals("deluser")) {
            delUser(request, response);
        } else if (method.equals("upuser")) {
            upUser(request, response);
        } else if (method.equals("allfruit")) {
            allFruit(request, response);
        } else if (method.equals("findfruit")) {
            findFruit(request,response);
        } else if (method.equals("upfruit")) {
            upFruit(request, response);
        } else if (method.equals("addfruit")) {
            addFruit(request, response);
        } else if (method.equals("hotfruit")) {
            hotFruit(request, response);
        }
    }

    private void hotFruit(HttpServletRequest request, HttpServletResponse response) {
        List<Fruit> allFruits = fruitService.findHot();
        request.setAttribute("allfruit" ,allFruits);
        try {
            request.getRequestDispatcher("/allFruit.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void addFruit(HttpServletRequest request, HttpServletResponse response) {
        String fname = request.getParameter("fname");
        String spec = request.getParameter("spec");
        String priceStr = request.getParameter("price");
        double price = Double.parseDouble(priceStr);
        String t1 = request.getParameter("t1");
        String t2 = request.getParameter("t2");
        String inumStr = request.getParameter("inum");
        int inum = Integer.parseInt(inumStr);
        Fruit fruit = new Fruit(0, fname, spec, price, t1, t2, inum);
        int add = fruitService.add(fruit);
        try {
            response.sendRedirect(request.getContextPath() + "/BSServlet?method=allfruit");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void upFruit(HttpServletRequest request, HttpServletResponse response) {
        String fidStr = request.getParameter("fid");
        int fid = Integer.parseInt(fidStr);
        String fname = request.getParameter("fname");
        String spec = request.getParameter("spec");
        String upStr = request.getParameter("up");
        double up = Double.parseDouble(upStr);
        String t1 = request.getParameter("t1");
        String t2 = request.getParameter("t2");
        String inumStr = request.getParameter("inum");
        int inum = Integer.parseInt(inumStr);
        Fruit fruit = new Fruit(fid, fname, spec, up, t1, t2, inum);
        int update = fruitService.update(fruit);
        try {
            request.getRequestDispatcher("/BSServlet?method=allfruit").forward(request, response);
        } catch (IOException | ServletException e) {
            e.printStackTrace();
        }
    }

    private void findFruit(HttpServletRequest request, HttpServletResponse response) {
        String fidStr = request.getParameter("fid");
        int fid = Integer.parseInt(fidStr);
        Fruit fruit = fruitService.findById(fid);
        request.setAttribute("fruit", fruit);
        try {
            request.getRequestDispatcher("/fruit.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void allFruit(HttpServletRequest request, HttpServletResponse response) {
        List<Fruit> allFruits = fruitService.getAll();
        request.setAttribute("allfruit" ,allFruits);
        try {
            request.getRequestDispatcher("/allFruit.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void upUser(HttpServletRequest request, HttpServletResponse response) {
        String uidStr = request.getParameter("uid");
        String name2 = request.getParameter("name2");
        String email2 = request.getParameter("email2");
        String phone2 = request.getParameter("phone2");
        String pwd2 = request.getParameter("pwd2");
        int uid = Integer.parseInt(uidStr);
        User user = new User(uid, email2, phone2, pwd2, name2);
        int update = userService.update(user);
        try {
            request.getRequestDispatcher("/BSServlet?method=alluser").forward(request, response);
        } catch (IOException | ServletException e) {
            e.printStackTrace();
        }
    }

    private void delUser(HttpServletRequest request, HttpServletResponse response) {
        String uidStr = request.getParameter("uid");
        int uid = Integer.parseInt(uidStr);
        int del = userService.del(uid);
        try {
            request.getRequestDispatcher("/BSServlet?method=alluser").forward(request, response);
        } catch (IOException | ServletException e) {
            e.printStackTrace();
        }
    }

    private void findUser(HttpServletRequest request, HttpServletResponse response) {
        String uidStr = request.getParameter("uid");
        int uid = Integer.parseInt(uidStr);
        User user = userService.findById(uid);
        request.setAttribute("user", user);
        try {
            request.getRequestDispatcher("/user.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void addUser(HttpServletRequest request, HttpServletResponse response) {
        String name1 = request.getParameter("name1");
        String email1 = request.getParameter("email1");
        String pwd1 = request.getParameter("pwd1");
        String phone1 = request.getParameter("phone1");
        User user = new User(email1, phone1, pwd1, name1);
        User add = userService.add(user);
        if (add!=null) {
            try {
                request.getRequestDispatcher("/BSServlet?method=alluser").forward(request, response);
            } catch (IOException | ServletException e) {
                e.printStackTrace();
            }
        }
    }

    private void allUser(HttpServletRequest request, HttpServletResponse response) {
        List<User> all = userService.findAll();
        request.setAttribute("allusers", all);
        try {
            request.getRequestDispatcher("/allUser.jsp").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void toAdmin(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if (user!=null) {
            try {
                response.sendRedirect(request.getContextPath() + "/BSindex.jsp");
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            request.setAttribute("showError", true);
            try {
                request.getRequestDispatcher("/login.jsp").forward(request, response);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}
